package com.youlu.campus.web.membership;

import com.youlu.campus.entity.VO.PlaceOrderReq;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.membership.service.UserMemberShipInfoService;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;


@RestController
public class MemberShipController {
    @Autowired
    private UserMemberShipInfoService userMemberShipInfoService;

    /**
     * 会员卡基本信息
     *
     * @return
     */
    @GetMapping("/mini-app/member-ship")
    public QResult memberShip() {
        return QResult.success(userMemberShipInfoService.getUserMemberShipInfo(RequestContext.getUserId()));
    }

    /**
     * 会员卡下单,传入商品编码和会员卡ID
     *
     * @param req
     * @param httpRequest
     * @return
     */
    @PostMapping("/mini-app/member-ship/place")
    public QResult placeMemberShip(@RequestBody @Valid PlaceOrderReq req, HttpServletRequest httpRequest) {
        req.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        req.setUserId(RequestContext.getUserId());
        if(Objects.isNull(req.getSource())){
            req.setSource(1);
        }
        return QResult.success(userMemberShipInfoService.placeOrder(req));
    }

}
